home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / gcctest / tests05.zoo / tregex.c < prev    next >
C/C++ Source or Header  |  1993-03-02  |  9KB  |  370 lines

  1. /*
  2.  * Simple test program for regexp(3) stuff.  Knows about debugging hooks.
  3.  *
  4.  *    Copyright (c) 1986 by University of Toronto.
  5.  *    Written by Henry Spencer.  Not derived from licensed software.
  6.  *
  7.  *    Permission is granted to anyone to use this software for any
  8.  *    purpose on any computer system, and to redistribute it freely,
  9.  *    subject to the following restrictions:
  10.  *
  11.  *    1. The author is not responsible for the consequences of use of
  12.  *        this software, no matter how awful, even if they arise
  13.  *        from defects in it.
  14.  *
  15.  *    2. The origin of this software must not be misrepresented, either
  16.  *        by explicit claim or by omission.
  17.  *
  18.  *    3. Altered versions must be plainly marked as such, and must not
  19.  *        be misrepresented as being the original software.
  20.  *
  21.  * Usage: try re [string [output [-]]]
  22.  * The re is compiled and dumped, regexeced against the string, the result
  23.  * is applied to output using regsub().  The - triggers a running narrative
  24.  * from regexec().  Dumping and narrative don't happen unless DEBUG.
  25.  *
  26.  * If there are no arguments, stdin is assumed to be a stream of lines with
  27.  * five fields:  a r.e., a string to match it against, a result code, a
  28.  * source string for regsub, and the proper result.  Result codes are 'c'
  29.  * for compile failure, 'y' for match success, 'n' for match failure.
  30.  * Field separator is tab.
  31.  */
  32. #include <stdio.h>
  33. #include <regexp.h>
  34.  
  35. /* build test strings in ++jrb */
  36. char *test_input[] = {
  37.     "abc    abc    y    &    abc",
  38.     "abc    xbc    n    -    -",
  39.     "abc    axc    n    -    -",
  40.     "abc    abx    n    -    -",
  41.     "abc    xabcy    y    &    abc",
  42.     "abc    ababc    y    &    abc",
  43.     "ab*c    abc    y    &    abc",
  44.     "ab*bc    abc    y    &    abc",
  45.     "ab*bc    abbc    y    &    abbc",
  46.     "ab*bc    abbbbc    y    &    abbbbc",
  47.     "ab+bc    abbc    y    &    abbc",
  48.     "ab+bc    abc    n    -    -",
  49.     "ab+bc    abq    n    -    -",
  50.     "ab+bc    abbbbc    y    &    abbbbc",
  51.     "ab?bc    abbc    y    &    abbc",
  52.     "ab?bc    abc    y    &    abc",
  53.     "ab?bc    abbbbc    n    -    -",
  54.     "ab?c    abc    y    &    abc",
  55.     "^abc$    abc    y    &    abc",
  56.     "^abc$    abcc    n    -    -",
  57.     "^abc    abcc    y    &    abc",
  58.     "^abc$    aabc    n    -    -",
  59.     "abc$    aabc    y    &    abc",
  60.     "^    abc    y    &    ",
  61.     "$    abc    y    &    ",
  62.     "a.c    abc    y    &    abc",
  63.     "a.c    axc    y    &    axc",
  64.     "a.*c    axyzc    y    &    axyzc",
  65.     "a.*c    axyzd    n    -    -",
  66.     "a[bc]d    abc    n    -    -",
  67.     "a[bc]d    abd    y    &    abd",
  68.     "a[b-d]e    abd    n    -    -",
  69.     "a[b-d]e    ace    y    &    ace",
  70.     "a[b-d]    aac    y    &    ac",
  71.     "a[-b]    a-    y    &    a-",
  72.     "a[b-]    a-    y    &    a-",
  73.     "a[b-a]    -    c    -    -",
  74.     "a[]b    -    c    -    -",
  75.     "a[    -    c    -    -",
  76.     "a]    a]    y    &    a]",
  77.     "a[]]b    a]b    y    &    a]b",
  78.     "a[^bc]d    aed    y    &    aed",
  79.     "a[^bc]d    abd    n    -    -",
  80.     "a[^-b]c    adc    y    &    adc",
  81.     "a[^-b]c    a-c    n    -    -",
  82.     "a[^]b]c    a]c    n    -    -",
  83.     "a[^]b]c    adc    y    &    adc",
  84.     "ab|cd    abc    y    &    ab",
  85.     "ab|cd    abcd    y    &    ab",
  86.     "()ef    def    y    &-\\1    ef-",
  87.     "()*    -    c    -    -",
  88.     "*a    -    c    -    -",
  89.     "^*    -    c    -    -",
  90.     "$*    -    c    -    -",
  91.     "(*)b    -    c    -    -",
  92.     "$b    b    n    -    -",
  93.     "a\\    -    c    -    -",
  94.     "a\\(b    a(b    y    &-\\1    a(b-",
  95.     "a\\(*b    ab    y    &    ab",
  96.     "a\\(*b    a((b    y    &    a((b",
  97.     "a\\\\b    a\\b    y    &    a\\b",
  98.     "abc)    -    c    -    -",
  99.     "(abc    -    c    -    -",
  100.     "((a))    abc    y    &-\\1-\\2    a-a-a",
  101.     "(a)b(c)    abc    y    &-\\1-\\2    abc-a-c",
  102.     "a+b+c    aabbabc    y    &    abc",
  103.     "a**    -    c    -    -",
  104.     "a*?    -    c    -    -",
  105.     "(a*)*    -    c    -    -",
  106.     "(a*)+    -    c    -    -",
  107.     "(a|)*    -    c    -    -",
  108.     "(a*|b)*    -    c    -    -",
  109.     "(a+|b)*    ab    y    &-\\1    ab-b",
  110.     "(a+|b)+    ab    y    &-\\1    ab-b",
  111.     "(a+|b)?    ab    y    &-\\1    a-a",
  112.     "[^ab]*    cde    y    &    cde",
  113.     "(^)*    -    c    -    -",
  114.     "(ab|)*    -    c    -    -",
  115.     ")(    -    c    -    -",
  116.     "    abc    y    &    ",
  117.     "abc        n    -    -",
  118.     "a*        y    &    ",
  119.     "([abc])*d    abbbcd    y    &-\\1    abbbcd-c",
  120.     "([abc])*bcd    abcd    y    &-\\1    abcd-a",
  121.     "a|b|c|d|e    e    y    &    e",
  122.     "(a|b|c|d|e)f    ef    y    &-\\1    ef-e",
  123.     "((a*|b))*    -    c    -    -",
  124.     "abcd*efg    abcdefg    y    &    abcdefg",
  125.     "ab*    xabyabbbz    y    &    ab",
  126.     "ab*    xayabbbz    y    &    a",
  127.     "(ab|cd)e    abcde    y    &-\\1    cde-cd",
  128.     "[abhgefdc]ij    hij    y    &    hij",
  129.     "^(ab|cd)e    abcde    n    x\\1y    xy",
  130.     "(abc|)ef    abcdef    y    &-\\1    ef-",
  131.     "(a|b)c*d    abcd    y    &-\\1    bcd-b",
  132.     "(ab|ab*)bc    abc    y    &-\\1    abc-a",
  133.     "a([bc]*)c*    abc    y    &-\\1    abc-bc",
  134.     "a([bc]*)(c*d)    abcd    y    &-\\1-\\2    abcd-bc-d",
  135.     "a([bc]+)(c*d)    abcd    y    &-\\1-\\2    abcd-bc-d",
  136.     "a([bc]*)(c+d)    abcd    y    &-\\1-\\2    abcd-b-cd",
  137.     "a[bcd]*dcdcde    adcdcde    y    &    adcdcde",
  138.     "a[bcd]+dcdcde    adcdcde    n    -    -",
  139.     "(ab|a)b*c    abc    y    &-\\1    abc-ab",
  140.     "((a)(b)c)(d)    abcd    y    \\1-\\2-\\3-\\4    abc-a-b-d",
  141.     "[a-zA-Z_][a-zA-Z0-9_]*    alpha    y    &    alpha",
  142.     "^a(bc+|b[eh])g|.h$    abh    y    &-\\1    bh-",
  143.     "(bc+d$|ef*g.|h?i(j|k))    effgz    y    &-\\1-\\2    effgz-effgz-",
  144.     "(bc+d$|ef*g.|h?i(j|k))    ij    y    &-\\1-\\2    ij-ij-j",
  145.     "(bc+d$|ef*g.|h?i(j|k))    effg    n    -    -",
  146.     "(bc+d$|ef*g.|h?i(j|k))    bcdd    n    -    -",
  147.     "(bc+d$|ef*g.|h?i(j|k))    reffgz    y    &-\\1-\\2    effgz-effgz-",
  148.     "((((((((((a))))))))))    -    c    -    -",
  149.     "(((((((((a)))))))))    a    y    &    a",
  150.     "multiple words of text    uh-uh    n    -    -",
  151.     "multiple words    multiple words, yeah    y    &    multiple words",
  152.     "(.*)c(.*)    abcde    y    &-\\1-\\2    abcde-ab-de",
  153.     "\\((.*), (.*)\\)    (a, b)    y    (\\2, \\1)    (b, a)",
  154.     "abcd    abcd    y    &-\\&-\\\\&    abcd-&-\\abcd",
  155.     "a(bc)d    abcd    y    \\1-\\\\1-\\\\\\1    bc-\\1-\\bc",
  156.     "[ -~]*    abc    y    &    abc",
  157.     "[ -~ -~]*    abc    y    &    abc",
  158.     "[ -~ -~ -~]*    abc    y    &    abc",
  159.     "[ -~ -~ -~ -~]*    abc    y    &    abc",
  160.     "[ -~ -~ -~ -~ -~]*    abc    y    &    abc",
  161.     "[ -~ -~ -~ -~ -~ -~]*    abc    y    &    abc",
  162.     "[ -~ -~ -~ -~ -~ -~ -~]*    abc    y    &    abc",
  163.   (char *)NULL
  164. };
  165.  
  166. #ifdef ERRAVAIL
  167. char *progname;
  168. extern char *mkprogname();
  169. #endif
  170.  
  171. #ifdef DEBUG
  172. extern int regnarrate;
  173. #endif
  174.  
  175. char buf[BUFSIZ];
  176.  
  177. int errreport = 0;        /* Report errors via errseen? */
  178. char *errseen = NULL;        /* Error message. */
  179. int status = 0;            /* Exit status. */
  180.  
  181. /* ARGSUSED */
  182. int main(argc, argv)
  183. int argc;
  184. char *argv[];
  185. {
  186.     regexp *r;
  187.     int i;
  188.  
  189. #ifdef ERRAVAIL
  190.     progname = mkprogname(argv[0]);
  191. #endif
  192.  
  193.     if (argc == 1) {
  194.         multiple();
  195.         return(status);
  196.     }
  197.  
  198.     r = regcomp(argv[1]);
  199.     if (r == NULL)
  200.         error("regcomp failure", "");
  201. #ifdef DEBUG
  202.     regdump(r);
  203.     if (argc > 4)
  204.         regnarrate++;
  205. #endif
  206.     if (argc > 2) {
  207.         i = regexec(r, argv[2], 1);
  208.         printf("%d", i);
  209.         for (i = 1; i < NSUBEXP; i++)
  210.             if (r->startp[i] != NULL && r->endp[i] != NULL)
  211.                 printf(" \\%d", i);
  212.         printf("\n");
  213.     }
  214.     if (argc > 3) {
  215.         regsub(r, argv[3], buf);
  216.         printf("%s\n", buf);
  217.     }
  218.     return(status);
  219. }
  220.  
  221. void
  222. regerror(s)
  223. char *s;
  224. {
  225.     if (errreport)
  226.         errseen = s;
  227.     else
  228.         error(s, "");
  229. }
  230.  
  231. #ifndef ERRAVAIL
  232. error(s1, s2)
  233. char *s1;
  234. char *s2;
  235. {
  236.     fprintf(stderr, "regexp: ");
  237.     fprintf(stderr, s1, s2);
  238.     fprintf(stderr, "\n");
  239.     exit(1);
  240. }
  241. #endif
  242.  
  243. int lineno;
  244.  
  245. regexp badregexp;        /* Implicit init to 0. */
  246.  
  247. char rbuf[1024];
  248. multiple()
  249. {
  250.     char *field[5];
  251.     char *scan;
  252.     int i, j;
  253.     regexp *r;
  254.     extern char *strchr();
  255.  
  256.     errreport = 1;
  257.     lineno = 0;
  258.         for(j = 0; test_input[j]; j++) {
  259.         strcpy(rbuf, test_input[j]);
  260.         lineno++;
  261.         scan = rbuf;
  262.         for (i = 0; i < 5; i++) {
  263.             field[i] = scan;
  264.             if (field[i] == NULL) {
  265.                 complain("bad testfile format", "");
  266.                 exit(1);
  267.             }
  268.             scan = strchr(scan, '\t');
  269.             if (scan != NULL)
  270.                 *scan++ = '\0';
  271.         }
  272.         try(field);
  273.     }
  274.  
  275.     /* And finish up with some internal testing... */
  276.     lineno = 9990;
  277.     errseen = NULL;
  278.     if (regcomp((char *)NULL) != NULL || errseen == NULL)
  279.         complain("regcomp(NULL) doesn't complain", "");
  280.     lineno = 9991;
  281.     errseen = NULL;
  282.     if (regexec((regexp *)NULL, "foo", 1) || errseen == NULL)
  283.         complain("regexec(NULL, ...) doesn't complain", "");
  284.     lineno = 9992;
  285.     r = regcomp("foo");
  286.     if (r == NULL) {
  287.         complain("regcomp(\"foo\") fails", "");
  288.         return;
  289.     }
  290.     lineno = 9993;
  291.     errseen = NULL;
  292.     if (regexec(r, (char *)NULL, 1) || errseen == NULL)
  293.         complain("regexec(..., NULL) doesn't complain", "");
  294.     lineno = 9994;
  295.     errseen = NULL;
  296.     regsub((regexp *)NULL, "foo", rbuf);
  297.     if (errseen == NULL)
  298.         complain("regsub(NULL, ..., ...) doesn't complain", "");
  299.     lineno = 9995;
  300.     errseen = NULL;
  301.     regsub(r, (char *)NULL, rbuf);
  302.     if (errseen == NULL)
  303.         complain("regsub(..., NULL, ...) doesn't complain", "");
  304.     lineno = 9996;
  305.     errseen = NULL;
  306.     regsub(r, "foo", (char *)NULL);
  307.     if (errseen == NULL)
  308.         complain("regsub(..., ..., NULL) doesn't complain", "");
  309.     lineno = 9997;
  310.     errseen = NULL;
  311.     if (regexec(&badregexp, "foo", 1) || errseen == NULL)
  312.         complain("regexec(nonsense, ...) doesn't complain", "");
  313.     lineno = 9998;
  314.     errseen = NULL;
  315.     regsub(&badregexp, "foo", rbuf);
  316.     if (errseen == NULL)
  317.         complain("regsub(nonsense, ..., ...) doesn't complain", "");
  318. }
  319.  
  320. try(fields)
  321. char **fields;
  322. {
  323.     regexp